package com.wss.common.widget.dialog;

import com.wss.common.base.ResourceTable;
import com.wss.common.widget.LVCircularRing;
import ohos.agp.components.Component;
import ohos.agp.components.DirectionalLayout;
import ohos.agp.components.LayoutScatter;
import ohos.agp.components.Text;
import ohos.agp.window.dialog.CommonDialog;
import ohos.app.Context;

public class LoadingDialog {

    private LVCircularRing mLoadingView;
    private CommonDialog mLoadingDialog;
    private Context context;
    private String msg = "加载中…";
    private boolean cancelable = true;
    private boolean isShow;

    public LoadingDialog(Context context) {
        this.context = context;
    }

    /**
     * 设置提示信息
     *
     * @param msg 提示信息
     * @return dialog
     */
    public LoadingDialog setTitleText(String msg) {
        this.msg = msg;
        return this;
    }

    /**
     * 设置dialog是否可以取消
     *
     * @param cancelable 是否可取消
     * @return dialog
     */
    public LoadingDialog setCancelable(boolean cancelable) {
        this.cancelable = cancelable;
        return this;
    }

    public void show() {
        Component view = LayoutScatter
                .getInstance(context)
                .parse(ResourceTable.Layout_dialog_loading, null, false);
        // 获取整个布局
        DirectionalLayout layout = (DirectionalLayout) view.findComponentById(ResourceTable.Id_dialog_view);
        // 页面中的LoadingView
        mLoadingView = (LVCircularRing) view.findComponentById(ResourceTable.Id_lvcr_loading);
        // 页面中显示文本
        Text loadingText = (Text) view.findComponentById(ResourceTable.Id_loading_text);
        // 显示文本
        loadingText.setText(msg);
        //TODO 创建自定义样式的Dialog
        mLoadingDialog = new CommonDialog(context);
        //TODO 设置返回键无效
//        mLoadingDialog.setCancelable(cancelable);
        mLoadingDialog.setContentCustomComponent(layout);
        mLoadingDialog.setTransparent(true);
        mLoadingDialog.show();
        mLoadingView.startAnim();
        isShow = true;
    }

    public void dismiss() {
        if (mLoadingDialog != null && isShow) {
            mLoadingView.stopAnim();
            mLoadingDialog.destroy();
            mLoadingDialog = null;
            isShow = false;
        }
    }

    public boolean isShowing() {
        return isShow;
    }
}
